from pynput import keyboard,mouse


def on_click(x,y, button, pressed): 
    global xgap,ygap,status,start_xy
    xgap=0
    ygap=0
    start_xy = mouse.Controller().position
    status=1
    deal(0,0)
    return

mouse_listener = mouse.Listener(on_click=on_click)
mouse_listener.start()

from global_values import gg

start_xy = (0,0)
xgap = 0
ygap = 0
screen_geo = (0,0,1365,767)
status = 1  # 0 normal, 1 two arrows,2 vertical,3 horiznal
def deal_1():
    global start_xy,xgap,ygap
    curp = mouse.Controller().position
    print('curp',curp,start_xy)
    if start_xy !=curp: 
        xgap =0
        ygap=0
        start_xy = mouse.Controller().position
    else:
        mouse.Controller().position = (start_xy[0]+xgap,start_xy[1]+ygap)
        start_xy = (start_xy[0]+xgap,start_xy[1]+ygap)
        xgap = 0
        ygap = 0
    print('startp',start_xy)

last_sta1_co = 0
g_co = 0
def deal_one(xx):
    global start_xy,xgap,ygap,status,last_sta1_co,g_co
    fno,type,code,value = xx
    g_co += 1
    dx = 0
    dy = 0
    if type==2 and code == 0:
        dx = value
    elif type==2 and code == 1:
        dy = value
    elif type==1 and value==1:
        if code==272:
            status=1
            deal_1()
            last_sta1_co = g_co
        if code==273:
            if status==1:
                deal_1()
                deal(0,0)
            if status==3:
                status=0
            else:
                status = 3
        if code==274:
            if status==1:
                deal_1()
                deal(0,0)
            status = 2
        if code==275:
            if status==1:
                deal_1()
                deal(0,0)
                time.sleep(0.1)
            mouse.Controller().click(mouse.Button.left)
    else:
        g_co -= 1
        
    dx,dy = mm[fno].put(dx,dy)
    return dx,dy

def deal(dx,dy):
    if status==0:
        mouse.Controller().move(dx,dy)
        return
    if status==2:
        mouse.Controller().move(0,dy)
        return
    if status==3:
        mouse.Controller().move(dx,0)
        return

    if status==1:
        global xgap,ygap
        xgap += dx
        if xgap+start_xy[0]>screen_geo[2]:
            xgap = screen_geo[2]-start_xy[0]
        if xgap+start_xy[0]<screen_geo[0]:
            xgap = screen_geo[0]-start_xy[0]
        ygap += dy

        if ygap+start_xy[1]>screen_geo[3]:
            ygap = screen_geo[3]-start_xy[1]
        if ygap+start_xy[1]<screen_geo[1]:
            ygap = screen_geo[1]-start_xy[1]
        if xgap==0 and ygap==0:
            gg.statusL[0] = (False,0,0)
            gg.statusL[1] = (False,0,0)
        else:            
            gg.statusL[0] = (True,start_xy[0]+xgap,min(start_xy[1],screen_geo[3]-20))
            gg.statusL[1] = (True,min(start_xy[0],screen_geo[2]-20),start_xy[1]+ygap)


def is_abnor(fno,type,code,value):
    global move_y,move_y_remains
    if type==2 and code in [0,1,8]:
        return False
    print(type,value,code)
    if type==1 and value==1:
        if code==273:            
            if move_y==0:
                move_y=2
            else:
                move_y=0
        if code==272:
            mouse.Controller().click(mouse.Button.left)            
            # if move_y==2:
            #     move_y=0
            # else:
            #     move_y=2

        if code==275:
#            move_y = 1            
            global all_effect
            all_effect = not all_effect

        if code==274:
            if move_y ==3:
                move_y = 1
            else:  
                move_y = 3

    print('move_y',move_y)
    return True


last_time = 0
import time
from rmove import rmove
from redis_get import get_bulk

scales = [1]
mm = {}
for i,s in enumerate(scales):
    mm[i] = rmove(s)

def do_one():
    dx,dy,fno,is_scroll = get_bulk()
    x,y = mouse.Controller().position
    if not is_scroll:
        if y+dy >750:
            dy = max(750-y,0)
        if x+dx>1360:
            dx = max(1360-x,0)
    mouse.Controller().move(dx,dy)

def work():
    while True:
        t1 = time.time()
        ret = get_bulk()
        block_time = time.time()-t1
        if block_time>0.8:
            print('clear',time.time())
            for k,v in mm.items():
                v.clear()
        xsum = 0
        ysum = 0

        for i in ret:
            dx,dy = deal_one(i)
            xsum += dx
            ysum += dy
        deal(xsum,ysum)
if __name__ =='__main__':
    work()